package org.netbeans.modules.qcubedpalette.qlabel;

import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import org.netbeans.modules.qcubedpalette.PaletteUtilities;
import org.openide.text.ActiveEditorDrop;

/**
 * QLabel
 * The Java representation of the QCodo QLabel class
 */
public class QLabel implements ActiveEditorDrop {

	private String name = "";
	private String text = "";

	public QLabel() {
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getName() {
		return this.name;
	}

	public String getText() {
		return this.text;
	}

	private String createMethod() {
		return "\nprotected function " + this.name + "_Create() {"
				+ "\n$this->" + this.name + " = new QLabel($this);"
				+ "\n$this->" + this.name + "->Text = '" + this.text + "';"
				+ "\n}\n";
	}

	private String createMethodCall() {
		return "\n$this->" + this.name + "_Create();\n";

	}

	private String createDataMember() {
		return "\nprotected $" + this.name + ";\n";
	}

	@Override
	public boolean handleTransfer(JTextComponent targetComponent) {
		QLabelCustomizer qlc = new QLabelCustomizer(this);
		boolean accept = qlc.showDialog();

		if (accept) {
			try {
				String method = createMethod();
				String dataMember = createDataMember();
				String methodCall = this.createMethodCall();
				PaletteUtilities.insertAt(method, targetComponent, targetComponent.getCaret().getDot());
				PaletteUtilities.insertAt(dataMember, targetComponent, PaletteUtilities.findClassMemberInsertionOffset(targetComponent.getDocument()));
				PaletteUtilities.insertAt(methodCall, targetComponent, PaletteUtilities.findFormCreateInsertionOffset(targetComponent.getDocument()));
			} catch (BadLocationException ble) {
				accept = false;
			}
		}
		return accept;
	}
}
